<?php
namespace app\api\controller;
use app\common\BaseController;
use app\api\model\TeamIncome as TeamIncomeModel;
use app\api\model\Receipt as ReceiptModel;
use app\common\Logs;
use think\Db;
class TeamIncome extends BaseController{    
    public function __construct(TeamIncomeModel $model){
        parent::__construct();
        $this->model = $model;
    }
     //查询
     public function index(){        
        $params = $this->request->param();
        //type,1：团队，2：散拼，3：项目
        if(empty($params['type'])){
            return '缺少type参数';
        }
        if(empty($params['parent_id'])){
            return '缺少parent_id参数';
        }            
        $where['type']=$params['type'];
        $where['parent_id'] = $params['parent_id'];   
        $data = $this->model->with(['receipts'=>function($query) use ($where){$query->where('type',$where['type']);},
        'client'=>'client',
        'guest'=>'guest',       
        'kind'=>'kind',
        'salesman'=>'salesman'
        ])->where($where)->order('id', 'desc')->select();
        foreach ($data as &$v) {
            $v['receipt_review']=0;
            foreach($v['receipts'] as $r){
                if($r['is_review']){
                    $v['receipt_review']=1;
                }
            }                
        }
        return $data;
    }
    //添加
    public function create(){        
        $param = $this->request->param(); 
        $params = json_decode($param['params'],true);        
        $operator_id = json_decode($param['operator_id'],true);
        //type,1：团队，2：散拼，3：项目
        if(empty($params['type'])){
            return '缺少type参数';
        }
        if(empty($params['parent_id'])){
            return '缺少parent_id参数';
        }        
        $data = $this->model->allowField(true)->isUpdate(false)->data($params)->save();        
        if($data){
            $team = Db::name("team")->where('id',$this->model->parent_id)->find();
            Logs::add("新增团队收入记录",$operator_id,$team['sn'], null,json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        }
        return $this->model;
    }
    //编辑
    public function update(){        
        $param = $this->request->param(); 
        $params = json_decode($param['params'],true);        
        $operator_id = json_decode($param['operator_id'],true);
        $old = $this->model->where('id',$params['id'])->find();
        $data = $this->model->allowField(true)->isUpdate(true)->save($params,['id',$params['id']]);
        if($data){
            $team = Db::name("team")->where('id',$this->model->parent_id)->find();
            Logs::add("修改团队收入记录",$operator_id,$team['sn'], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        }
        return $this->model;
    }
    //删除
    public function delete(){
        $params = $this->request->param();
        $receipt = new ReceiptModel();
        $income_data = $this->model->where('id',$params['id'])->find();        
        $receipt_data = $receipt->where(["income_id"=>$income_data["id"],"type"=>$income_data['type']])->select();
        foreach($receipt_data as $item){
            $receipt->where(["id"=>$item["id"],"type"=>$item['type']])->delete();
        }     
        $old = $this->model->allowField(true)->where('id',$params['id'])->find();
        $data = $this->model->allowField(true)->where('id',$params['id'])->delete();
        if($data){
            $team = Db::name("team")->where('id',$old['parent_id'])->find();
            Logs::add("删除团队收入记录",$params['operator_id'],$team['sn'], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        }
        return $data;
    }
        //审核
        public function review(){            
            $param = $this->request->param();             
            $params = $param['params'];        
            $operator_id = $param['operator_id'];
            $old = $this->model->where('id',$params)->find();            
            $data=$this->model->where('id',$params)->update(['is_review'=>'1']);  
            if($data){ 
                $team = Db::name("team")->where('id',$old['parent_id'])->find();  
                $new=$this->model->whereIn('id',$params)->find();              
                Logs::add("审核团队收入记录",$operator_id,$team['sn'], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($new,JSON_UNESCAPED_UNICODE)); 
            }
            return $data;
        }
        //取消审核
        public function cancel_review(){
            $param = $this->request->param();
            $params = $param['params'];        
            $operator_id = $param['operator_id'];
            $old = $this->model->where('id',$params)->find();
            $data=$this->model->where('id',$params)->update(['is_review'=>'0']);  
            if($data){   
                $team = Db::name("team")->where('id',$old['parent_id'])->find();  
                $new=$this->model->whereIn('id',$params)->find();   
                Logs::add("解除审核团队收入记录",$operator_id,$team['sn'], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($new,JSON_UNESCAPED_UNICODE));  
            }
            return $data;
        }

}